//controlls
define(function (require, exports, module) {
    const Vue=require("vue");
    var template=require('text!./index.html');
    const api=require("api");
    const sdk=require("sdk");
    const weui=require('weui');
    const utils=require('utils');
    module.exports=Vue.extend({
        template:template,
        data:function () {
            return {
                deposit_amount:0
            }
        },
        created:function () {

        },
        computed:{
            oauth:function () {
                return this.$store.getters.getOAuth;
            },
            isBack:function () {
                return this.$store.getters.getIsBack;
            },
            user_info:function () {
                return this.$store.getters.getUserInfo;
            },
            sdkInit:function () {
                return this.$store.getters.getSdkInit;
            }
        },
        beforeRouteEnter:function (to,from,next) {
            next(function (vm) {
                if(!vm.isBack){
                    vm.getDeposit();
                }
            })
        },
        methods:{
            confirm:function (title,callback) {
                var v=this;
                weui.confirm(title, {
                    title: '',
                    buttons: [{
                        label: '取消',
                        type: 'default',
                        onClick: function(){ v.$router.push({ path: '/index' }); }
                    }, {
                        label: '确定',
                        type: 'primary',
                        onClick: function(){ callback();}
                    }]
                });
            },
            //提交押金
            postDeposit:function () {
                var v=this;
                if(!v.oauth.open_id||v.oauth.open_id==="undefined"){
                    if(utils.isWeixn()) {
                        window.location.href = utils.oauthUrl();
                    }
                    else {
                        weui.topTips("请在微信中使用此功能！");
                    }
                    return;
                }
                if(parseInt(v.deposit_amount)<=0){
                    v.getDeposit();
                    return;
                }
                var loading = weui.loading('提交中……');
                api.chargeDeposit("1",v.deposit_amount,"0",v.oauth.open_id,v.user_info).then(function (data) {
                    if(data.user_token_invalid){
                        v.$store.commit("logOut");
                        v.$router.push({path: '/phone'});
                        return;
                    }
                    if(data.user_charge_deposit&&data.user_charge_deposit.return_code==="SUCCESS"){
                        var sdkObj=JSON.parse(data.user_charge_deposit.return_msg);
                        var adopt_seriNo=sdkObj.adopt_seriNo;
                        //拿到支付订单信息 调用支付接口
                        sdk.pay2(sdkObj).then(function (res) {
                            if(/:ok$/i.test(res.err_msg)){
                                //充值成功提交
                                if(adopt_seriNo)
                                api.chargeDespositStatus(adopt_seriNo,"1",res.err_msg,v.user_info);
                                v.$store.dispatch('UserInfo', v.user_info);
                                weui.toast("押金缴纳成功！",{callback:function () {
                                    v.$router.push({ path: '/index' });
                                }});
                            }
                        }).catch(function (e) {
                            //充值失败提交
                            if(adopt_seriNo)
                            api.chargeDespositStatus(adopt_seriNo,"0",res.err_msg,v.user_info);
                            if(/:cancel$/i.test(e.err_msg)){
                                weui.toast("取消支付！");
                            }else if(/:fail/i.test(e.err_msg)){
                                weui.toast("支付失败！");
                            }
                        });
                    }else {
                        return Promise.reject(data.user_charge_deposit.return_msg);
                    }
                }).catch(function (e) {
                    weui.topTips(e);
                }).finally(function () {
                    loading.hide();
                });
            },
            getDeposit:function () {
                var v=this;
                api.getChargeDeposit(v.user_info).then(function (res) {
                    if(res.user_token_invalid){
                        v.$store.commit("logOut");
                        v.$router.push({path: '/phone'});
                        return;
                    }
                    if(res.get_deposit_amount&&res.get_deposit_amount.return_code==="SUCCESS"){
                        v.deposit_amount=res.get_deposit_amount.deposit_amount;
                    }else {
                        weui.topTips('押金获取失败！');
                    }
                }).catch(function (e) {
                    weui.topTips("网络异常，请刷新页面再试!");
                });
            }
        }
    });
});